#include "AutoImportWindow.h"
#include <QStyle>
#include <QFileDialog>
#include "ImportProcessWindow.h"


AutoImportWindow::AutoImportWindow(QWidget *parent) : QWidget(parent)
{
    noteLabel = new QLabel(this);
    noteLabel->setOpenExternalLinks(true);
    noteLabel->setText(QString(tr("note_text")).arg("https://1drv.ms/u/s!Aje4GqNXlnOXg7pEmaJHf0s8Uq9hKw?e=BJeIAm", "https://pan.baidu.com/s/1By74AERc7cAZ_HjFQRctAw", "orac", "https://pan.quark.cn/s/66e75161150f"));

    addressEdit = new QLineEdit(this);
    addressEdit->setText(tr("select_zip_file"));
    addressEdit->setFixedWidth(350);

    selectFileButton = new QPushButton(this);
    selectFileButton->setIcon(QIcon::fromTheme(QIcon::ThemeIcon::FolderOpen));
    connect(selectFileButton, &QPushButton::clicked, this, [this](bool)
    {
        auto path = QFileDialog::getOpenFileName(nullptr, tr("open_file_title"), "", "(*.ora)");
        addressEdit->setText(path);
    });

    importButton = new QPushButton(this);
    importButton->setText(tr("import_button"));
    connect(importButton, &QPushButton::clicked, this, [this](bool)
    {
        auto sub_window = new ImportProcessWindow(addressEdit->text(), this);
        sub_window->setAttribute(Qt::WA_DeleteOnClose);
        sub_window->setWindowModality(Qt::WindowModal);
        sub_window->show();
    });
    
    importLayout = new QHBoxLayout();
    importLayout->addStretch();
    importLayout->addWidget(addressEdit);
    importLayout->addWidget(selectFileButton);
    importLayout->addStretch();
    importLayout->addWidget(importButton);
    importLayout->addStretch();

    mainLayout = new QVBoxLayout();
    mainLayout->addWidget(noteLabel);
    mainLayout->addStretch();
    mainLayout->addLayout(importLayout);
    mainLayout->addStretch();

    setLayout(mainLayout);
    setWindowFlags(Qt::Window);
    setWindowTitle(tr("auto_import_window_title"));
    setWindowIcon(QIcon::fromTheme(QIcon::ThemeIcon::FolderOpen));
    setFixedSize(500, 144);
}

AutoImportWindow::~AutoImportWindow()
{
    delete mainLayout;
    delete noteLabel;
    delete addressEdit;
    delete selectFileButton;
    delete importButton;
}
